// 获取购物车
const setShoppingList = (state, shoppingList) => {
	if(shoppingList != null) {
		for(var i = 0; i < shoppingList.length; i++) {
			shoppingList[i].url = "/goodsDetail/" + shoppingList[i].goods_id;
			// 每个商品默认不挑选
			shoppingList[i].is_select = false;
		}
	}
	state.shoppingList = shoppingList
}
// 设置默认规格下标
const setDefaultSpecIndex = (spec) => {
	for(var i = 0; i < spec.length; i++) {
		if(spec[i].goods_storage > 0) {
			return i;
		}
	}
}
// 获取购物车详情
const setShoppingDetail = (state, goodsDetails) => {
	// 设置默认选择的规格
	goodsDetails.defaultSpecIndex = setDefaultSpecIndex(goodsDetails.spec);
	// 有搭配购普通商品 设置默认选中的规格
	if(goodsDetails.goods_label == 0 && goodsDetails.collocation && goodsDetails.collocation.length > 0) {
		for(var i = 0; i < goodsDetails.collocation.length; i++) {
			goodsDetails.collocation[i].defaultSpecIndex = setDefaultSpecIndex(goodsDetails.collocation[i].spec)
		}
	}
	// 替每个小商品添加 count属性
	for(var i = 0; i < goodsDetails.spec.length; i++) {
		goodsDetails.spec[i].count = 1;
	}
	if(goodsDetails.goods_label === 3) {
		switch(goodsDetails.groupbuy_info.button_text) {
			case "众筹未开始":
				goodsDetails.groupbuy_info.groupState = 10; //未开始
				_this._countLeftovertTimeDisc(goodsDetails.groupbuy_info.count_down);
				break;
			case "众筹中":
				//				goodsDetails.groupbuy_info.countDownText = _getCountDownText(goodsDetails.groupbuy_info.count_down).countDownText;
				goodsDetails.groupbuy_info.groupState = 20; //进行中
				break;
			case "众筹已结束":
				goodsDetails.groupbuy_info.groupState = 30; // 结束
				break;
				// case "时间到了已结束":
				//   goodsDetails.groupbuy_info.groupState = 32;
				//   goodsDetails.groupbuy_info.countDownText = "众筹已成功";
				//   break;
		}
	}
	// 每次进来清除数组
	state.memberAvatarList = [];
	if(goodsDetails.target_quantity) {
		for(var i = 0; i < goodsDetails.target_quantity; i++) {
			state.memberAvatarList.push("../../../../static/img/default_group_avatar.svg");
		}
	}
	state.goodsDetails = goodsDetails
}

// 获取购物车多个订单
const setSelectGoods = (state, data) => {
	state.shoppingSelectGoods = data.shoppingSelectGoods
	state.shoppingSelectGoods.param = data.param
}
// 月定制计算月份
const _getSerializeMonth = (state) => {
	var _date = [],
		dateData = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
	//准备一个月份反转的数组
	var dateDataRet = Object.assign([], dateData).reverse();
	//获取当前年份
	var yearText = new Date().getFullYear();
	//获取当前月份  调试的时候，大家可以通过调整now调试  3月-now=2,12月now=11...
	var nowDate = (new Date()).getDate();
	// var nowMonth = 11;
	var nowMonth = new Date().getMonth() - 1;
	var startMonth = nowMonth;
	if(state.goodsDetails.delivery_time <= nowDate) {
		if(nowMonth == 11) {
			startMonth = 0;
		} else {
			startMonth = nowMonth + 1;
		}
	}
	for(let i = 0; i < 12; i++) {
		if(startMonth - i < 0) {
			//如果startMonth-i<0，从dateDataRet里面拿数据，下标=|startMonth-i|-1。
			_date.push({
				year: yearText,
				month: dateDataRet[Math.abs(startMonth - i) - 1],
				count: 0
			});
		} else {
			//从dateData里面拿数据，下标=startMonth-i
			_date.push({
				year: yearText + 1,
				month: dateData[startMonth - i],
				count: 0
			});
		}
	}
	_date.reverse();
	state.goodsDetails.serializeMonth = _date;
}
export default {
	setShoppingList,
	setShoppingDetail,
	setSelectGoods,
	_getSerializeMonth
}